﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kimd.Motion
{
    public interface IAxis
    {
        bool IsError { get; }
        string LastError { get; }
        bool[] IsHomeFinish { get; set; }
        bool GetAxisHomeFound(short cardNo, short axisNo);
        bool ClearOffset(short cardNo, short axisNo);
        void SetAxisServoOn(short cardNo, short axisNo);
        void SetAxisServoOff(short cardNo, short axisNo);
        AxisStatus GetAxisStatus(short cardNo, short axisNo);
        void ClearAxisStatus(short cardNo, short axisNo);
        double GetAxisCommandPosition(short cardNo, short axisNo);
        double GetAxisEncoderPosition(short cardNo, short axisNo);
        void ZeroAxisEncoder(short cardNo, short axisNo);
        void ZeroAxisCommand(short cardNo, short axisNo);
        double VelUnitConvert(double vel);
        double AccUnitConvert(double vel, double acc);
        bool AxisMoveContinous(short cardNo, short axisNo, MoveDirection direction, AxisSpeed axisSpeed);
        bool AxisMoveAbsolute(short cardNo, short axisNo, int distanceInPulse, AxisSpeed axisSpeed);
        bool AxisMoveRelative(short cardNo, short axisNo, int offsetInPulse, AxisSpeed axisSpeed);
        bool AxisGoHome(short cardNo, short axisNo, short homeMode, bool homeDir, AxisSpeed homeSpeed);
        bool AxisChangeSpeed(short cardNo, short axisNo, AxisSpeed changedSpeed);
        void StopAxis(short cardNo, short axisNo, bool now);
        StopType GetStopReason(short cardNo, short axisNo);
        void ResetServoWarn(short cardNo, short axisNo);
        bool CheckApiResult(short rtn, string errMessage);
    }
    public class AxisStatus
    {
        public bool ServoOn { get; set; }
        public bool ServoWarn { get; set; }
        public bool PositiveLimit { get; set; }
        public bool NegativeLimit { get; set; }
        public bool Origin { get; set; }
        public bool EmergencyStop { get; set; }
        public bool Ready { get; set; }
        public bool ReachPosition { get; set; }
        public bool Home { get; set; }
    }
    public class AxisSpeed
    {
        public double VelStart;
        public double VelEnd;
        public double Vel;
        public double Acc;
        public double Dec;
        public double STime;
        public override string ToString()
        {
            return $"{VelStart},{Vel},{VelEnd},{Acc},{Dec},{STime}";
        }
        public void FromString(string str)
        {
            string[] strs = str.Split(',');
            VelStart = Convert.ToDouble(strs[0]);
            Vel = Convert.ToDouble(strs[1]);
            VelEnd = Convert.ToDouble(strs[2]);
            Acc = Convert.ToDouble(strs[3]);
            Dec = Convert.ToDouble(strs[4]);
            STime = Convert.ToDouble(strs[5]);
        }
    }
}
